<?php
require_once(dirname(__FILE__)."/../config.php");
$name=$array['remark'];
$pwd=$array['openid'];
if(empty($dopost)) $dopost =' login';
if(empty($fmdo)) $fmdo = 'login';
if($fmdo=='login')
{
    //用户登录
    if($dopost=="login")
    {
        
        
        
        if(!isset($vdcode))
        {
            $vdcode = '';
        }
        $svali = GetCkVdValue();
        if(preg_match("/2/",$safe_gdopen)){
            if(strtolower($vdcode)!=$svali || $svali=='')
            {
                ResetVdValue();
                ShowMsg('验证码错误！', '-1');
                exit();
            }

        }
        if(CheckUserID($userid,'',false)!='ok')
        {
            ShowMsg("你输入的用户名 {$userid} 不合法！","-1");
            // header("Location:/member/login.php");
            exit();
        }
        if($pwd=='')
        {
            ShowMsg("密码不能为空！","-1",1,2000);
            // header("Location:/member/login.php");
            exit();
        }

        //检查帐号
        $rs = $cfg_ml->CheckUser($userid,$pwd);

        #api{{
        if(defined('UC_API') && @include_once DEDEROOT.'/uc_client/client.php')
        {
            //检查帐号
            list($uid, $userid, $pwd, $email) = uc_user_login($userid, $pwd);
            if($uid > 0) {
                //当UC存在用户,而CMS不存在时,就注册一个
                if(!$rs) {
                    //会员的默认金币
                    $row = $dsql->GetOne("SELECT `money`,`scores` FROM `#@__arcrank` WHERE `rank`='10' ");
                    $scores = is_array($row) ? $row['scores'] : 0;
                    $money = is_array($row) ? $row['money'] : 0;
                    $logintime = $jointime = time();
                    $loginip = $joinip = GetIP();
                    $res = $dsql->ExecuteNoneQuery("INSERT INTO #@__member SET `mtype`='个人',`userid`='$userid',`pwd`='$pwd',`uname`='',`sex`='男' ,`rank`='10', `jointime`='$jointime',`joinip`='$joinip',`logintime`='$logintime',`loginip`='$loginip';");
                    if($res) {
                        $mid = $dsql->GetLastID();
                        $data = array
                        (
                            0 => "INSERT INTO `#@__member_person` SET `mid`='$mid', `onlynet`='1', `sex`='男', `uname`='$username', `qq`='', `msn`='', `tel`='', `mobile`='', `place`='', `oldplace`='0' ,
                                 `birthday`='1980-01-01', `star`='1', `income`='0', `education`='0', `height`='160', `bodytype`='0', `blood`='0', `vocation`='0', `smoke`='0', `marital`='0', `house`='0',
                       `drink`='0', `datingtype`='0', `language`='', `nature`='', `lovemsg`='', `address`='',`uptime`='0';",
                            1 => "INSERT INTO `#@__member_tj` SET `mid`='$mid',`article`='0',`album`='0',`archives`='0',`homecount`='0',`pagecount`='0',`feedback`='0',`friend`='0',`stow`='0';",
                            2 => "INSERT INTO `#@__member_space` SET `mid`='$mid',`pagesize`='10',`matt`='0',`spacename`='{$uname}的空间',`spacelogo`='',`spacestyle`='person', `sign`='',`spacenews`='';",
                            3 => "INSERT INTO `#@__member_flink` SET `mid`='$mid', `title`='DEDEBIZ管理系统', `url`='https://www.dedebiz.com';"
                        );
                        foreach($data as $val) $dsql->ExecuteNoneQuery($val);
                    }
                }
                $rs = 1;
                $row = $dsql->GetOne("SELECT `mid`, `pwd` FROM #@__member WHERE `userid`='$username'");
                if(isset($row['mid']))
                {
                    $cfg_ml->PutLoginInfo($row['mid']);
                    if($pwd!=$row['pwd']) $dsql->ExecuteNoneQuery("UPDATE #@__member SET `pwd`='$pwd' WHERE mid='$row[mid]'");
                }
                //生成同步登录的代码
                $ucsynlogin = uc_user_synlogin($uid);
            } else if($uid == -1) {
                //当UC不存在该用而CMS存在,就注册一个.
                if($rs) {
                    $row = $dsql->GetOne("SELECT `email` FROM #@__member WHERE userid='$userid'");
                    $uid = uc_user_register($userid, $pwd, $row['email']);
                    if($uid > 0) $ucsynlogin = uc_user_synlogin($uid);
                } else {
                    $rs = -1;
                }
            } else {
                $rs = -1;
            }
        }
        #/aip}}

        if($rs==0)
        {
            //ShowMsg("用户名不存在！", "-1", 0, 2000);
            header("Location:/appdemo/login.php");
            exit();
        }
        else if($rs==-1) {
            //ShowMsg("密码错误！", "-1", 0, 2000);
            header("Location:/appdemo/login.php");
            exit();
        }
        else if($rs==-2) {
            //ShowMsg("管理员帐号不允许从前台登录！", "-1", 0, 2000);
            header("Location:/appdemo/login.php");
            exit();
        }
        else
        {
            // 清除会员缓存
            $cfg_ml->DelCache($cfg_ml->M_ID);
            if(empty($gourl) || preg_match("#action|_do#i", $gourl))
            {
                //ShowMsg("成功登录，5秒钟后转向系统主页...","index.php",0,2000);
                header("Location:/appdemo/index.php");exit();
            }
            else
            {
                $gourl = str_replace('^','&',$gourl);
                //ShowMsg("成功登录，现在转向指定页面...",$gourl,0,2000);
                header("Location:$gourl");exit();
            }
            exit();
        }
    }

    //退出登录
    else if($dopost=="exit")
    {
        $cfg_ml->ExitCookie();
        #api{{
        if(defined('UC_API') && @include_once DEDEROOT.'/uc_client/client.php')
        {
            $ucsynlogin = uc_user_synlogout();
        }
        #/aip}}
//        ShowMsg("成功退出登录！","index.php",0,2000);
        header("Location:/appdemo/login.php");exit();
    }
}
    ?>